<script>
	import {mapMutations} from "vuex"
	export default {
		methods:{
			...mapMutations(['login', 'logout'])
		},
		
		onLaunch() {
				//在页面加载时读取sessionStorage里的状态信息
				if (sessionStorage.getItem("store") ) {
					let stateData = JSON.parse(sessionStorage.getItem("store"));
					//// 检测登录是否过期 验证token是否失效 失效则重新登录
					if (stateData.token === "" || stateData.email === "") {
						this.logout()
						uni.clearStorage(); // 清除缓存
						this.$router.push("/users/signin");
					} else {
						uniCloud.callFunction({
							name: "validateToken",
							data: {
								token:stateData.token
							}
						}).then(res => {
							if (res.result.status !== 0) {
								this.logout()
								uni.clearStorage(); // 清除缓存
								this.$router.push("/users/signin");
							} else { // 验证成功更新登录状态
								// this.$store.replaceState(Object.assign({}, this.$store.state,stateData));
								let {hasLogin,token,email,relevanceUserID} = stateData
								this.login({hasLogin,token,email,relevanceUserID});
								this.$router.push( uni.getStorageSync('curRouter') || "/");
							}
						})
					}
					//删除 状态信息
					sessionStorage.clear();
				} else {
					uni.clearStorage(); // 清除缓存
					this.$router.push("/users/signin");
				}
				
				//在页面刷新时将vuex里的信息保存到sessionStorage里
				window.addEventListener("beforeunload",()=>{
				    sessionStorage.setItem("store",JSON.stringify(this.$store.state))
				})		
		},
		onShow() {},
		onHide() {}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
	}
	// 全局视图切换动画
	.v-enter {
		opacity: 0;
		transform: translateX(50%);
	}
	
	.v-leave-to {
		opacity: 0;
		transform: translateX(-50%);
	}
	
	.v-leave-active,
	.v-enter-active {
		transition: all 0.5s ease-in-out;
	}
	
	//定位
	.position_father{
		position: relative;
	}
	.position_child{
		position: absolute;
	}
	
	//自定义滚动条
	.custom-scrollbar{
		overflow-y: auto;
		overflow-x: auto;
		/*定义滚动条高宽及背景
		 高宽分别对应横竖滚动条的尺寸*/
		&::-webkit-scrollbar
		{
		    width:6px;
		    height:6px;
		}
		/*定义滚动条轨道
		 内阴影+圆角*/
		// &::-webkit-scrollbar-track
		// {
		//     -webkit-box-shadow:inset 0 0 6px rgba(0,0,0,0.3);
		//     border-radius:10px;
		//     background-color:#F5F5F5;
		// }
		/*定义滑块
		 内阴影+圆角*/
		&::-webkit-scrollbar-thumb
		{
		    border-radius:10px;
		    -webkit-box-shadow:inset 0 0 6px rgba(0,0,0,.3);
		    background-color:#ccc;
		}
	}
</style>
